package logic

import (
	"context"
	"encoding/json"
	"yundian/internel/app/dao"
	"yundian/internel/app/models/paysetting"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)

type SettingLogic struct{}

func NewSettingLogic() *SettingLogic {
	return &SettingLogic{}
}

func (logic *SettingLogic) GetPaySetting() *paysetting.PaySetting {
	var settInfo *paysetting.PaySetting
	// 判断redis中是否存在
	s := global.Rdb.Get(context.Background(), utils.Join(global.Cfg.Wx.PayTypeName, "-paysetting")).Val()
	if s == "" {
		settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByTypeName(global.Cfg.Wx.PayTypeName).Unwrap().(*paysetting.PaySetting)
		global.Rdb.Set(context.Background(), utils.Join(global.Cfg.Wx.PayTypeName, "-paysetting"), settInfo, 0)
	} else {
		_ = json.Unmarshal([]byte(s), &settInfo)
	}
	//log.Println("打印测试信息---配置", settInfo, settInfo.MchId)
	return settInfo
}

func (logic *SettingLogic) GetPaySettingByTypeName(typeName string) *paysetting.PaySetting {
	var settInfo *paysetting.PaySetting
	// 判断redis中是否存在
	s := global.Rdb.Get(context.Background(), utils.Join(typeName, "-paysetting")).Val()
	if s == "" {
		settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByTypeName(typeName).Unwrap().(*paysetting.PaySetting)
		global.Rdb.Set(context.Background(), utils.Join(typeName, "-paysetting"), settInfo, 0)
	} else {
		_ = json.Unmarshal([]byte(s), &settInfo)
	}
	//log.Println("打印测试信息---配置", settInfo, settInfo.MchId)
	return settInfo
}

func (logic *SettingLogic) GetPaySettingByAppid(appId string) *paysetting.PaySetting {
	var settInfo *paysetting.PaySetting
	settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByAppid(appId).Unwrap().(*paysetting.PaySetting)
	return settInfo
}

func (logic *SettingLogic) GetPaySettingByAppidAndMchid(appId, mchid string) *paysetting.PaySetting {
	var settInfo *paysetting.PaySetting
	settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByAppidAndMchid(appId, mchid).Unwrap().(*paysetting.PaySetting)
	return settInfo
}

func (logic *SettingLogic) GetAliPaySetting() *paysetting.PaySetting {
	var settInfo *paysetting.PaySetting
	// 判断redis中是否存在
	s := global.Rdb.Get(context.Background(), utils.Join(global.Cfg.Ali.PayTypeName, "-paysetting")).Val()
	if s == "" {
		settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByTypeName(global.Cfg.Ali.PayTypeName).Unwrap().(*paysetting.PaySetting)
		global.Rdb.Set(context.Background(), utils.Join(global.Cfg.Ali.PayTypeName, "-paysetting"), settInfo, 0)
	} else {
		_ = json.Unmarshal([]byte(s), &settInfo)
	}

	return settInfo

}
